package com.sky.controller;

import com.sky.aspectj.annotation.Log;
import com.sky.aspectj.enums.BusinessType;

import com.sky.domain.WxUser;
import com.sky.dto.UserPwdUpdateDto;
import com.sky.dto.WxUserDto;
import com.sky.dto.WxUserLoginDto;
import com.sky.service.WxUserService;
import com.sky.vo.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author majinan
 */
@RestController
@RequestMapping("/wxUser")
@Api(tags = {"微信用户管理"})
public class WxUserController {

    @Autowired
    private WxUserService userService;


    /**
     * 添加
     */
    @PostMapping("addUser")
    @Log(title = "添加用户",businessType = BusinessType.INSERT)
    public AjaxResult addUser(@Validated WxUserDto userDto) {
        int i = this.userService.addUser(userDto);
        if (i == 0) {
            return AjaxResult.fail("手机号已存在");
        }
        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("updateUser")
    @Log(title = "修改用户",businessType = BusinessType.UPDATE)
    public AjaxResult updateUser(@Validated WxUserDto userDto) {
        return AjaxResult.toAjax(this.userService.updateUser(userDto));
    }

    /**
     * 根据ID查询一个用户信息
     */
    @GetMapping("getUserById/{userId}")
    public AjaxResult getUserById(@PathVariable @Validated @NotNull(message = "用户ID不能为空") Long userId) {
        return AjaxResult.success(this.userService.getOne(userId));
    }



    /**
     * 重置密码
     */
    @PutMapping("resetPwd/{userIds}")
    public AjaxResult resetPwd(@PathVariable Long[] userIds){
        if(userIds.length > 0){
            this.userService.resetPwd(userIds);
            return AjaxResult.success("重置成功");
        }
        return AjaxResult.fail("重置失败,没有选择用户");
    }

    /**
     * 修改密码
     */
    @PutMapping("updatePwd")
    public AjaxResult updatePwd(@RequestBody @Validated UserPwdUpdateDto dto){
        return AjaxResult.toAjax(this.userService.updatePwd(dto));
    }

    @PostMapping("doLogin")
    @Log(title = "用户登录")
    public AjaxResult doLogin(@Validated WxUserLoginDto userDto) {
        WxUser login = userService.login(userDto);
        if (login == null){
            return AjaxResult.fail("用户名密码不正确");
        }
        return AjaxResult.success(login);
    }

}
